主题
匹配动画窗口1 - MatchAnimationFromPtr
函数简介
匹配动画窗口,可用于GIF动画识别,在指定时间内识别动图,查找到立即返回结果,返回相对绑定窗口坐标。模板通过内存指针传入。
接口名称
MatchAnimationFromPtrDLL调用
long MatchAnimationFromPtr(long ola, int x1, int y1, int x2, int y2, long templ, double matchVal, int type, double angle, double scale, int delay, int time, int threadCount);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| x1 | 整数型 | 查找区域的左上角X坐标 |
| y1 | 整数型 | 查找区域的左上角Y坐标 |
| x2 | 整数型 | 查找区域的右下角X坐标 |
| y2 | 整数型 | 查找区域的右下角Y坐标 |
| templ | 长整数型 | OLAImage对象的地址,由LoadImage等接口生成 |
| matchVal | 双精度浮点数 | 相似度阈值,范围0-1,如0.85表示85%相似度 |
| type | 整数型 | 匹配类型:1-灰度匹配(速度快);2-彩色匹配;3-透明匹配;4-透明彩色权重匹配;5-普通彩色匹配 |
| angle | 双精度浮点数 | 旋转角度,每次匹配后旋转指定角度继续匹配,角度越小匹配次数越多时间越长,0为不旋转速度最快 |
| scale | 双精度浮点数 | 窗口缩放比例,默认为1,可通过GetScaleFromWindows接口获取 |
| delay | 整数型 | 动画帧间隔,单位毫秒 |
| time | 整数型 | 总识别时间,单位毫秒 |
| threadCount | 整数型 | 用于查找的线程数,需根据delay帧率自行调整,过小会导致识别时间到期未识别完,过大会导致CPU占用过高 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long templPtr = ola.LoadImage("img/template.bmp");
if (templPtr != 0) {
MatchData result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if (result.MatchState) {
int x = result.X;
int y = result.Y;
}
// 后续不再使用该模板图时释放
ola.FreeImage(templPtr);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long templPtr = ola.LoadImage("img/template.bmp");
if (templPtr != 0)
{
var result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if (result.MatchState)
{
int x = result.X;
int y = result.Y;
}
// 后续不再使用该模板图时释放
ola.FreeImage(templPtr);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
templ_ptr = ola.LoadImage("img/template.bmp")
if templ_ptr:
result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4)
if result.get("MatchState"):
x = result["X"]
y = result["Y"]
# 后续不再使用该模板图时释放
ola.FreeImage(templ_ptr)java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.MatchResult;
import java.util.List;
OLAPlugServer ola = new OLAPlugServer();
long templPtr = ola.LoadImage("img/template.bmp");
if (templPtr != 0) {
MatchResult result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if (result != null && result.MatchState) {
int x = result.X;
int y = result.Y;
}
// 后续不再使用该模板图时释放
ola.FreeImage(templPtr);
}cpp
var ola = com("OlaPlug.OlaSoft")
var templPtr = ola.LoadImage("img/template.bmp")
if(templPtr) {
var result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4)
if(result.MatchState) {
var x = result.X
}
// 后续不再使用该模板图时释放
ola.FreeImage(templPtr)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
templPtr = ola.LoadImage("img/template.bmp")
If templPtr <> 0 Then
Set result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4)
If result.MatchState Then
' 后续不再使用该模板图时释放
ola.FreeImage(templPtr)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
templPtr = ola.LoadImage ("img/template.bmp")
.如果真 (templPtr ≠ 0)
result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4)
.如果真 (result.MatchState)
' 后续不再使用该模板图时释放
ola.FreeImage (templPtr)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var templPtr = ola.LoadImage("img/template.bmp");
if(templPtr){
var result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if(result.MatchState){
var x = result.X;
}
// 后续不再使用该模板图时释放
ola.FreeImage(templPtr);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 templPtr = ola.LoadImage("img/template.bmp")
如果真 (templPtr ≠ 0)
{
MatchData result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4)
如果真 (result.MatchState)
' 后续不再使用该模板图时释放
ola.FreeImage(templPtr)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long templPtr = ola.LoadImage("img/template.bmp");
if (templPtr != 0) {
MatchData result = ola.MatchAnimationFromPtr(0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if (result.MatchState) {
int32_t x = result.X;
int32_t y = result.Y;
}
// 后续不再使用该模板图时释放
ola.FreeImage(templPtr);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long ptr = MatchAnimationFromPtr(instance, 0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if (ptr != 0) {
int matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
double matchVal = 0, angle = 0;
char json[4096] = {0};
GetStringFromPtr(ptr, json, sizeof(json));
ParseMatchImageJson(instance, json, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
FreeStringPtr(instance, ptr);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long MatchAnimationFromPtr(long ola, int x1, int y1, int x2, int y2, long templ, double matchVal, int type, double angle, double scale, int delay, int time, int threadCount);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ParseMatchImageJson(string str, out int matchState, out int x, out int y, out int width, out int height, out double matchVal, out double angle, out int index);
long instance = CreateCOLAPlugInterFace();
long ptr = MatchAnimationFromPtr(instance, 0, 0, 0, 0, templPtr, 0.9, 1, 0, 1, 200, 3000, 4);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
ParseMatchImageJson(sb.ToString(), out int matchState, out int x, out int y, out int w, out int h, out double mv, out double ang, out int idx);
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.MatchAnimationFromPtr(instance, ...)
# 原生返回 JSON 字符串指针,可用 ParseMatchImageJson 解析返回值
字符串指针,返回JSON格式的匹配结果。DLL调用返回字符串指针地址,需要调用 FreeStringPtr 接口释放内存。
返回数据格式:
json
{
"MatchVal": 0.85,
"MatchState": true,
"Index": 0,
"Angle": 45.0,
"X": 100,
"Y": 200,
"Width": 100,
"Height": 100
}| 字段名 | 类型 | 说明 |
|---|---|---|
| MatchVal | 浮点数 | 匹配相似度。 |
| MatchState | 布尔 | 是否匹配成功。 |
| Index | 整数 | 结果索引(从 0 开始)。 |
| Angle | 浮点数 | 匹配角度。 |
| X | 整数 | X 坐标。 |
| Y | 整数 | Y 坐标。 |
| Width | 整数 | 宽度。 |
| Height | 整数 | 高度。 |
注意事项
- x1、y1、x2、y2都传0时,将搜索整个窗口客户区
- 识别结果最长等待时间为 time + 1000ms
- 返回的坐标是相对于绑定窗口客户区的坐标
